import json

import requests
from django.http import HttpResponse


# Create your views here.

def getOpenId(request):
    """
    url = /user/getOpenId
    @param request: code
    @return: HttpResponse(openid)
    获取用户提交的临时code，向微信api查询用户标识符openid
    """
    code = request.GET['code']
    url = "https://api.weixin.qq.com/sns/jscode2session"
    url += "?appid=wx240df597557ea60a"
    url += "&secret=9d2f1c2210af524ea3d28b081bda7249"
    url += "&js_code=" + code
    url += "&grant_type=authorization_code"
    url += "&connect_redirect=1"
    get_id_request = requests.get(url)
    openid = json.loads(get_id_request.text).get('openid')
    request.session['openid'] = openid
    request.session.set_expiry(3600)
    print(request.session.get('openid'))
    return HttpResponse(openid)
